<template>
    <div class="dashboard">
        <el-row :gutter="20">
            <el-col :xs="24" :sm="12" :lg="6">
                <el-card class="stat-card">
                    <div class="stat-content">
                        <div class="stat-icon user-icon">
                            <el-icon size="24">
                                <User />
                            </el-icon>
                        </div>
                        <div class="stat-info">
                            <h3>{{ stats.userCount }}</h3>
                            <p>用户总数</p>
                        </div>
                    </div>
                </el-card>
            </el-col>

            <el-col :xs="24" :sm="12" :lg="6">
                <el-card class="stat-card">
                    <div class="stat-content">
                        <div class="stat-icon role-icon">
                            <el-icon size="24">
                                <UserFilled />
                            </el-icon>
                        </div>
                        <div class="stat-info">
                            <h3>{{ stats.roleCount }}</h3>
                            <p>角色总数</p>
                        </div>
                    </div>
                </el-card>
            </el-col>

            <el-col :xs="24" :sm="12" :lg="6">
                <el-card class="stat-card">
                    <div class="stat-content">
                        <div class="stat-icon menu-icon">
                            <el-icon size="24">
                                <Menu />
                            </el-icon>
                        </div>
                        <div class="stat-info">
                            <h3>{{ stats.menuCount }}</h3>
                            <p>菜单总数</p>
                        </div>
                    </div>
                </el-card>
            </el-col>

            <el-col :xs="24" :sm="12" :lg="6">
                <el-card class="stat-card">
                    <div class="stat-content">
                        <div class="stat-icon online-icon">
                            <el-icon size="24">
                                <Monitor />
                            </el-icon>
                        </div>
                        <div class="stat-info">
                            <h3>{{ stats.onlineCount }}</h3>
                            <p>在线用户</p>
                        </div>
                    </div>
                </el-card>
            </el-col>
        </el-row>

        <el-row :gutter="20" style="margin-top: 20px;">
            <el-col :span="24">
                <el-card>
                    <template #header>
                        <div class="card-header">
                            <span>系统信息</span>
                        </div>
                    </template>
                    <div class="system-info">
                        <el-descriptions :column="2" border>
                            <el-descriptions-item label="系统名称">后台管理系统</el-descriptions-item>
                            <el-descriptions-item label="系统版本">v1.0.0</el-descriptions-item>
                            <el-descriptions-item label="服务器时间">{{ currentTime }}</el-descriptions-item>
                            <el-descriptions-item label="运行环境">Vue 3 + Element Plus</el-descriptions-item>
                            <el-descriptions-item label="数据库">SQLite</el-descriptions-item>
                            <el-descriptions-item label="后端框架">Flask</el-descriptions-item>
                        </el-descriptions>
                    </div>
                </el-card>
            </el-col>
        </el-row>

        <el-row :gutter="20" style="margin-top: 20px;">
            <el-col :span="24">
                <el-card>
                    <template #header>
                        <div class="card-header">
                            <span>快速操作</span>
                        </div>
                    </template>
                    <div class="quick-actions">
                        <el-button type="primary" @click="$router.push('/users')">
                            <el-icon>
                                <User />
                            </el-icon>
                            用户管理
                        </el-button>
                        <el-button type="success" @click="$router.push('/roles')">
                            <el-icon>
                                <UserFilled />
                            </el-icon>
                            角色管理
                        </el-button>
                        <el-button type="warning" @click="$router.push('/menus')">
                            <el-icon>
                                <Menu />
                            </el-icon>
                            菜单管理
                        </el-button>
                    </div>
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios'

const stats = reactive({
    userCount: 0,
    roleCount: 0,
    menuCount: 0,
    onlineCount: 1
})

const currentTime = ref('')

// 更新时间
const updateTime = () => {
    currentTime.value = new Date().toLocaleString('zh-CN')
}

// 获取统计数据
const fetchStats = async () => {
    try {
        // 这里可以创建专门的统计API，现在先用模拟数据
        stats.userCount = 10
        stats.roleCount = 3
        stats.menuCount = 8
        stats.onlineCount = 1
    } catch (error) {
        console.error('获取统计数据失败:', error)
    }
}

onMounted(() => {
    updateTime()
    fetchStats()

    // 每秒更新时间
    setInterval(updateTime, 1000)
})
</script>

<style scoped>
.dashboard {
    padding: 0;
}

.stat-card {
    height: 100px;
}

.stat-content {
    display: flex;
    align-items: center;
    height: 100%;
}

.stat-icon {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    margin-right: 15px;
}

.user-icon {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.role-icon {
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.menu-icon {
    background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.online-icon {
    background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.stat-info h3 {
    margin: 0;
    font-size: 24px;
    font-weight: bold;
    color: #303133;
}

.stat-info p {
    margin: 5px 0 0 0;
    font-size: 14px;
    color: #909399;
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.system-info {
    margin-top: 20px;
}

.quick-actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.quick-actions .el-button {
    margin: 0;
}
</style>